package com.dycx.message.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.message.converter.MessageRecordConverter;
import com.dycx.message.mapper.MessageRecordMapper;
import com.dycx.message.dto.MessageRecordDTO;
import com.dycx.message.entity.MessageRecordDO;
import com.dycx.message.service.MessageRecordService;
import com.dycx.message.vo.MessageRecordVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 消息发送记录(MessageRecord)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-11-15 11:02:30
 */

@Service
@AllArgsConstructor
public class MessageRecordImpl extends ServiceImpl<MessageRecordMapper, MessageRecordDO> implements MessageRecordService {

    private MessageRecordMapper messageRecordMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<MessageRecordVO> getPage(MessageRecordDTO dto) {
        LambdaQueryWrapper<MessageRecordDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getMessageType()), MessageRecordDO::getMessageType, dto.getMessageType());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getTemplateId()), MessageRecordDO::getTemplateId, dto.getTemplateId());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getResult()), MessageRecordDO::getResult, dto.getResult());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getRecipient()), MessageRecordDO::getRecipient, dto.getRecipient());
        wrapper.orderByDesc(MessageRecordDO::getId);

        Page<MessageRecordDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<MessageRecordVO> voPage = MessageRecordConverter.entityToVoPage(entityPage);
        voPage.getRecords().forEach(this::setData);

        return voPage;
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public MessageRecordVO load(Integer id) {
        MessageRecordDO entity = getById(id);

        return setData(MessageRecordConverter.entityToVo(entity));
    }

    /**
     * 设置数据
     *
     * @param vo 实体
     * @return 查询结果
     */
    public MessageRecordVO setData(MessageRecordVO vo) {
        return vo;
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(MessageRecordDTO dto) {
        MessageRecordDO entity = BeanUtil.toBean(dto, MessageRecordDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(MessageRecordDTO dto) {
        MessageRecordDO entity = BeanUtil.toBean(dto, MessageRecordDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return messageRecordMapper.getMaxSort();
    }
}

